let query_item_name = '';
let query_item_state = '';
let curr = 1;
let size = Number($('.select_page').val());
// console.log(size);
let pagenum = 0;
function createPagination(curr, total, size){
	pagenum = Math.ceil(total / size);
	let html = '';
	for(let i=1; i<= pagenum; i++){
		if(curr == i){
			html += `
					<li class='active'>${i}</li>
					`
		}else{
			html += `
					<li>${i}</li>
					`
		}
	}
	$('.list').empty().append(html);
}


function getItemList(){
	$.ajax({
		url: 'http://chonghekj.com/crud/index.php/Home/Index/getItemList',
		type: 'get',
		dataType: 'json',
		data: {
			pageSize: size,
			pageNumber: curr,
			item_name: query_item_name,
			item_state: query_item_state
		},
		success: function(res){
			console.log(res);
			let html = '';
			for(let i=0; i < res.rows.length; i++){
				html += `
						<tr>
							<td><input type='checkbox' class='select'></td>
							<td class='item_id'>${res.rows[i].item_id}</td>
							<td class='item_name'>${res.rows[i].item_name}</td>
							<td class='item_price'>${res.rows[i].item_price}</td>
							<td class='item_format'>${res.rows[i].item_format}</td>
							<td class='item_total'>${res.rows[i].item_total}</td>
							<td class='item_state'>${res.rows[i].item_state}</td>
							<td class='item_inventory'>${res.rows[i].item_inventory}</td>
							<td class='create_time'>${res.rows[i].create_time}</td>
							<td class='update_time'>${res.rows[i].update_time}</td>
						</tr>
						`
			}
			$('.surface').empty().append(html);
			$('.total').html(res.total);
			createPagination(curr, res.total, size);
		}
	})
}
getItemList();

$('.select_page').change(function(){
	size = Number($('.select_page').val());
	curr = 1;
	getItemList();
});

$('.list').on('click', 'li', function(){
	curr = Number($(this).text());
	getItemList();
});

$('.prev').click(function(){
	if(curr == 1){
		curr = 1;
	}else{
		curr = curr - 1;
		getItemList();
	}
})

$('.next').click(function(){
	if(curr < pagenum){
		curr = curr + 1;
		getItemList();
	}
})
$('.addTo').click(function(){
	$('.popup').css('top','50px');
	$('.popup').css('opacity','1');
	$('.popup').css('z-index','2');
	$('.filter').css('opacity','1');
	$('.filter').css('z-index','1');
})

$('.cancel').click(function(){
	$('.popup').css('top','-530px');
	$('.popup').css('opacity','0');
	$('.popup').css('z-index','0');
	$('.filter').css('opacity','0');
	$('.filter').css('z-index','-1');
})
$('.submit').click(function(){
	$('.popup').css('top','-530px');
	$('.popup').css('opacity','0');
	$('.popup').css('z-index','0');
	$('.filter').css('opacity','0');
	$('.filter').css('z-index','-1');
})
$('.submit2').click(function(){
	$('.popup2').css('top','-530px');
	$('.popup2').css('opacity','0');
	$('.popup2').css('z-index','0');
	$('.filter').css('opacity','0');
	$('.filter').css('z-index','-1');
})

// if($('.select').attr('checked','true')){
	$('.modify').click(function(){
		$('.popup2').css('top','50px');
		$('.popup2').css('opacity','1');
		$('.popup2').css('z-index','2');
		$('.filter').css('opacity','1');
		$('.filter').css('z-index','1');
	})
// }

$('.cancel').click(function(){
	$('.popup2').css('top','-530px');
	$('.popup2').css('opacity','0');
	$('.popup2').css('z-index','0');
	$('.filter').css('opacity','0');
	$('.filter').css('z-index','-1');
})

$('.submit').click(function(){
	let item_name = $('.add_item_name').val();
	let item_price = $('.add_item_unitPrice').val();
	let item_format = $('.add_item_company').val();
	let item_total = $('.add_item_number').val();
	let item_inventory = $('.add_item_forecast').val();
	
	$.ajax({
		url: 'http://chonghekj.com/crud/index.php/Home/Index/addItem',
		type: 'post',
		dataType: 'json',
		data: {
			item_name,
			item_price,
			item_format,
			item_total,
			item_inventory
		},
		success: function(res){
			// console.log(res)
			$('.add_item_name').val('');
			$('.add_item_unitPrice').val('');
			$('.add_item_company').val('');
			$('.add_item_number').val('');
			$('.add_item_forecast').val('');
			getItemList();
		}
	})
})

$('.surface').on('click', '.select', function(){
	let item = $('.select:checked');
	// console.log(item);
	let item_id = item.parent().parent().find('.item_id').text();
	console.log(item_id);
	let item_name = item.parent().parent().find('.item_name').text();
	let item_price = item.parent().parent().find('.item_price').text();
	let item_format = item.parent().parent().find('.item_format').text();
	let item_total = item.parent().parent().find('.item_total').text();
	let item_state = item.parent().parent().find('.item_state').text();
	let item_inventory = item.parent().parent().find('.item_inventory').text();
	let create_time = item.parent().parent().find('.create_time').text();
	let update_time = item.parent().parent().find('.update_time').text();
	
	$('.update_item_name').val(item_name);
	$('.update_item_unitPrice').val(item_price);
	$('.update_item_company').val(item_price);
	$('.update_item_number').val(item_total);
	$('.update_item_forecast').val(item_inventory);
$('.submit2').click(function(){
	item_name = $('.update_item_name').val();
	item_format = $('.update_item_unitPrice').val();
	item_total = $('.update_item_number').val();
	item_inventory = $('.update_item_forecast').val();
	$.ajax({
			url: 'http://chonghekj.com/crud/index.php/Home/Index/editItem',
			type: 'post',
			dataType: 'json',
			data: {
				item_id,
				item_name,
				item_format,
				item_total,
				item_inventory,
			},
			success: function(res){
				// item_name = $('.update_item_name').val();
				// console.log(res);
				getItemList();
			}
		})
	})
})

$('.delete').click(function(){
	let items = $('.select:checked');
	let ids = [];
	// console.log(item);
	for(let i=0; i < items.length; i++){
		ids.push($(items[i]).parent().parent().find('.item_id').text());
	}
	
	$.ajax({
		url: 'http://chonghekj.com/crud/index.php/Home/Index/deleteItem',
		type: 'post',
		dataType: 'json',
		data: {
			ids
		},
		success: function(res){
			ids = [];
			getItemList();
		}
	})
})

$('.query').click(function(){
	query_item_name = $('.name').val();
	query_item_state = $('.state').val();
	$('.name').val('');
	getItemList();
})

// $('.surface').on('click', '.select', function(){
		
// }